package com.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;

import com.domain.Bestelling;
import com.domain.Klant;
import com.domain.Product;
import com.interfaces.IAktieHuis;
import com.interfaces.IBestellingDAO;
import com.interfaces.IKlantDAO;
import com.interfaces.IProductDAO;

@Transactional(propagation= Propagation.REQUIRED, readOnly=false)
@Service("aktieHuisService")
public class AktieHuis implements IAktieHuis{
	private Map<Integer,Bestelling> winkelwagens = new HashMap<Integer,Bestelling>(); 
	private IKlantDAO klantDAO;
	private IProductDAO productDAO;
	private IBestellingDAO bestellingDAO;
	private SessionFactory session;
	
	@Override
	public int aanmelden(String naam) {
		Klant klant = null;
		klant = klantDAO.vindByNaam(naam);
		if(klant == null){
			klant = new Klant(naam, klantDAO.getHoogsteKlantNr());
			klantDAO.voegToe(klant);
		}
		return klant.id;
		
	}

	@Override
	public List<String> getProductSoorten() {
		return productDAO.getProductSoorten();
	}

	@Override
	public List<Product> getProductLijst(String soort) {
		return productDAO.getProductLijst(soort);
	}

	@Override
	public int nieuweBestelling(int klantNr) {
		int temp = bestellingDAO.getHoogsteBestelNr();
		winkelwagens.put(klantNr,new Bestelling(temp));
		return temp;
	}

	@Override
	public boolean bestel(int klantNr, int bestelNr, int productCode, int aantal) {
		Bestelling bestelling = winkelwagens.get(klantNr);
		if(bestelling != null && bestelling.getId() == bestelNr){
			bestelling.add(productDAO.getProductByCode(productCode),aantal);
			return true;
		}
		return false;
	}

	@Override
	public Bestelling getBestelOverzicht(int klantNr, int bestelNr) {
		Bestelling bestelling = winkelwagens.get(klantNr); 
		return (bestelling != null && bestelling.getId()==bestelNr)?bestelling:null;
	}

	@Override
	public void bevestigBestelling(int klantNr, int bestelNr) {
		Bestelling bestelling = winkelwagens.get(klantNr); 
		if(bestelling != null && bestelling.getId()==bestelNr){
			bestellingDAO.voegToe(bestelling);
				winkelwagens.remove(klantNr);
		}
	}

	@Autowired
	public void setSessionFactory(SessionFactory session) {
		this.session = session;
	}

	public SessionFactory getSessionFactory() {
		return session;
	}

	@Autowired
	public void setKlantDAO(IKlantDAO dao) {
		klantDAO = dao;
	}

	public IKlantDAO getKLantDAO() {
		return klantDAO;
	}

	@Autowired
	public void setProductDAO(IProductDAO dao) {
		productDAO = dao;
	}

	public IProductDAO getProductDAO() {
		return productDAO;
	}

	@Autowired
	public void setBestellingDAO(IBestellingDAO dao) {
		bestellingDAO = dao;
		
	}

	public IBestellingDAO getBestellingDAO() {
		return bestellingDAO;
	}
	
}
